package site.xuqing.androidbase.net;

import android.accounts.NetworkErrorException;

import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import site.xuqing.androidbase.net.flash.IFlashViewManager;

/**
 * @author xuqing
 * @Description 网络请求框架二次封装observer
 * @createTime 2021年12月13日 14:12:17
 */
public abstract class BaseObserver<T> implements Observer<BaseResponse<T>> {
    /**
     * 是否需要延迟弹出等待加载组件，默认是
     */
    private boolean showFlashViewDelay=true;
    /**
     * 加载框弹出等待时间，默认600ms，当600ms网络还没成功，则弹出加载框
     */
    private long showFlashViewDelayTime=600;
    /**
     * 延迟加载组件管理器
     */
    private IFlashViewManager mFlashViewManager;
    
    /**
     * 谁知刷新管理器
     * @param flashViewManager flashViewManager
     * @return BaseObserver
     */
    public BaseObserver<T> setFlashViewManager(IFlashViewManager flashViewManager){
        mFlashViewManager=flashViewManager;
        return this;
    }
    
    /**
     * 是否延迟显示刷新框架
     * @param showFlashViewDelay 是否延迟，默认延迟
     * @return BaseObserver
     */
    public BaseObserver<T> isShowFlashViewDelay(boolean showFlashViewDelay){
        this.showFlashViewDelay=showFlashViewDelay;
        return this;
    }
    
    /**
     * 设置刷新框架延迟显示时间
     * @param millisecond 延迟时间，ms 默认600ms
     * @return BaseObserver
     */
    public BaseObserver<T> setShowFlashViewDelayTime(long millisecond){
        this.showFlashViewDelayTime=millisecond;
        return this;
    }
    
    @Override
    public void onSubscribe(@NonNull Disposable d) {
        if (mFlashViewManager!=null){
            if (!showFlashViewDelay) {
                mFlashViewManager.showImmediate();
            }else{
                mFlashViewManager.showDelay(showFlashViewDelayTime);
            }
        }
    }
    
    @Override
    public void onComplete() {
        if (mFlashViewManager!=null){
            mFlashViewManager.cancel();
        }
    }
    
    @Override
    public void onError(Throwable e) {
        if (mFlashViewManager!=null){
            mFlashViewManager.cancel();
        }
        if (e instanceof ConnectException ||
            e instanceof TimeoutException ||
            e instanceof NetworkErrorException ||
            e instanceof UnknownHostException) {
            try {
                onFailure(e, false);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } else {
            try {
                onFailure(e, true);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
    
    @Override
    public void onNext(@androidx.annotation.NonNull BaseResponse<T> tBaseResponse) {
        if (mFlashViewManager!=null){
            mFlashViewManager.cancel();
        }
        if (tBaseResponse.isSuccess()) {
            onSuccess(tBaseResponse);
        } else {
            onCodeError(tBaseResponse);
        }
    }
    
    /**
     * 成功
     * @param tBaseResponse tBaseResponse
     */
    public abstract void onSuccess(BaseResponse<T> tBaseResponse);
    
    /**
     * 服务器返回失败，有错误的状态码可处理
     * @param tBaseResponse tBaseResponse
     */
    public abstract void onCodeError(BaseResponse<T> tBaseResponse);
    
    /**
     * 错误，非服务器处理的错误
     * @param e 异常
     * @param netWork 是否网络异常
     * @throws Exception exception
     */
    public abstract void onFailure(Throwable e, boolean netWork) throws Exception;
}